﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;

/// <summary>
/// DateUtil 的摘要描述
/// </summary>
public static class DateUtil
{
    
    /// <summary>
    /// 轉換成yyyy-MM-dd-00-00-00
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static DateTime ConvertToZeroHour(this DateTime dt) {
        //DateTime newDt = dt;
        String dt_str=dt.ToString("yyyy-MM-dd")+"-00-00-00";

        return DateTime.ParseExact(dt_str,"yyyy-MM-dd-HH-mm-ss",null);

    }
    /// <summary>
    /// 轉換成yyyy-MM-dd-23-59-59
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static DateTime ConvertToMaxHour(this DateTime dt)
    {
        //DateTime newDt = dt;
        String dt_str = dt.ToString("yyyy-MM-dd") + "-23-59-59";

        return DateTime.ParseExact(dt_str, "yyyy-MM-dd-HH-mm-ss", null);

    }

    /// <summary>
    /// 轉換成yyyy-MM-dd
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string ConvertToShowDT(this object dt)
    {
        return ((DateTime)dt).ToString("yyyy-MM-dd");
    }

    /// <summary>
    /// 回傳民國年格式日期時間 100-02-14 08:00
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string ConvertToROCDT(this DateTime dt)
    {
        TaiwanCalendar tc = new TaiwanCalendar();

        return tc.GetYear(dt) + "-" + dt.ToString("MM-dd HH:mm");
    }

    /// <summary>
    /// 回傳民國年格式日期 100-02-14
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string ConvertToROC(this DateTime dt)
    {
        TaiwanCalendar tc = new TaiwanCalendar();

        return tc.GetYear(dt) + "-" + dt.ToString("MM-dd");
    }

    public static string TitleSubstring(this object val)
    {
        string str = (string)val;

        if (str.Length > 35)
        {
            return str.Substring(0, 35) + "...";
        }
        else
        {
            return str;
        }
    }

    public static string Language = System.Configuration.ConfigurationManager.AppSettings["WebLanguage"];

    /// <summary>
    /// 轉換為簡體中文
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static string ConverToCN(this object obj)
    {
        return ConverToCN((string)obj);
    }

    /// <summary>
    /// 轉換為簡體中文
    /// </summary>
    /// <param name="SourceString"></param>
    /// <returns></returns>
    public static string ConverToCN(this string SourceString)
    {
        if (!string.IsNullOrEmpty(SourceString))
        {
            string newString = string.Empty;

            if (Language == "zh-cn")
            {
                return ChineseConverter.Convert(SourceString, ChineseConversionDirection.TraditionalToSimplified);
            }
            else
            {
                return SourceString;
            }
        }
        else
        {
            return "";
        }
        

        //switch (Language)
        //{
        //    case "zh-tw":
        //        newString = ChineseConverter.Convert(SourceString, ChineseConversionDirection.SimplifiedToTraditional);
        //        break;
        //    case "zh-cn":
        //        newString = ChineseConverter.Convert(SourceString, ChineseConversionDirection.TraditionalToSimplified);
        //        break;
        //    default:
        //        newString = SourceString;
        //        break;
        //}
        //return newString;
    }

}